package com.lc.controller;

import com.alibaba.fastjson.JSONObject;
import com.lc.service.ApiService;
import com.lc.util.YyghException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "事务所管理接口")
@Controller
@RequestMapping
public class ApiController extends BaseController{
    @Autowired
    private ApiService apiService;


    @RequestMapping("/office/index")
    public String getOffice(ModelMap model, HttpServletRequest request, RedirectAttributes redirectAttributes)
    {
        try {
            JSONObject office = apiService.getOffice();
            model.addAttribute("office", apiService.getOffice());
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return "office/index";
    }
    @RequestMapping(value="/office/create")
    public String createHospital(ModelMap model)
    {
        System.out.println("添加事务所");
        return "office/create";
    }

    @RequestMapping(value="/office/save")
    public String saveOffice(String data,HttpServletRequest request)
    {
        JSONObject ob=JSONObject.parseObject(data);
        System.out.println("-------"+ob.toString());
        System.out.println("data数据"+data);

        try {

            apiService.saveOffice(data);

        }catch (YyghException e)
        {
            return this.failurePage(e.getMessage(),request);
        }catch(Exception e)
        {
             e.printStackTrace();
        }
        return this.successPage(null,request);
    }

@RequestMapping("lawer/list")
public String findDepartment(ModelMap model,
                             @RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize,
                             HttpServletRequest request,RedirectAttributes redirectAttributes) {
    try {
//        HospitalSet hospitalSet = hospitalSetMapper.selectById(1);
//        if(null == hospitalSet || StringUtils.isEmpty(hospitalSet.getHoscode()) || StringUtils.isEmpty(hospitalSet.getSignKey())) {
//            this.failureMessage("先设置医院code与签名key", redirectAttributes);
//            return "redirect:/hospitalSet/index";
//        }

        model.addAllAttributes(apiService.findLawer(pageNum, pageSize));
    } catch (YyghException e) {
        System.out.println("error:"+e);
        this.failureMessage(e.getMessage(), request);
    } catch (Exception e) {
        e.printStackTrace();
        this.failureMessage("数据异常", request);
    }
    return "lawer/index";
}

    @RequestMapping(value="/lawer/create")
    public String create(ModelMap model) {
        return "lawer/create";
    }

    @RequestMapping(value="/lawer/save",method= RequestMethod.POST)
    public String save(String data, HttpServletRequest request) {
        JSONObject ob=JSONObject.parseObject(data);
        System.out.println("-------"+ob.toString());
        System.out.println("data数据"+data);

        try {
            apiService.saveLawer(data);
        } catch (YyghException e) {
            return this.failurePage(e.getMessage(),request);
        } catch (Exception e) {
            System.out.println("输出数据异常的错误类型：");
            e.printStackTrace();
            return this.failurePage("数据异常",request);
        }
        return this.successPage(null,request);
    }
    @RequestMapping("/profession/list")
    public String findSchedule(ModelMap model,
                               @RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "10") int pageSize,
                               HttpServletRequest request,RedirectAttributes redirectAttributes) {
        try {
//            HospitalSet hospitalSet = hospitalSetMapper.selectById(1);
//            if(null == hospitalSet || StringUtils.isEmpty(hospitalSet.getHoscode()) || StringUtils.isEmpty(hospitalSet.getSignKey())) {
//                this.failureMessage("先设置医院code与签名key", redirectAttributes);
//                return "redirect:/hospitalSet/index";
//            }

            model.addAllAttributes(apiService.findProfession(pageNum, pageSize));
        } catch (YyghException e) {
            this.failureMessage(e.getMessage(), request);
        } catch (Exception e) {
            this.failureMessage("数据异常", request);
        }
        return "profession/index";
    }
    @RequestMapping(value="/profession/create")

    public String createSchedule(ModelMap model) {
        return "profession/create";
    }
    @RequestMapping(value="/profession/save",method=RequestMethod.POST)
    public String saveProfession(String data, HttpServletRequest request) {
        try {
            //data = data.replaceAll("\r\n", "").replace(" ", "");
            apiService.saveProfession(data);
        } catch (YyghException e) {
            return this.failurePage(e.getMessage(),request);
        } catch (Exception e) {
            e.printStackTrace();
            return this.failurePage("数据异常："+e.getMessage(),request);
        }
        return this.successPage(null,request);
    }

    @RequestMapping(value="/lawer/remove/{lawercode}/{officecode}",method=RequestMethod.GET)
    public String removeDepartment(ModelMap model, @PathVariable String lawercode, @PathVariable String officecode,RedirectAttributes redirectAttributes) {
        System.out.println("----------"+lawercode+officecode);
        try {
            apiService.removeLawer(lawercode,officecode);
        }catch(Exception e)
        {
            e.printStackTrace();
        }


        this.successMessage(null, redirectAttributes);
        return "redirect:/lawer/list";
    }
    @RequestMapping(value="/profession/remove/{professioncode}/{lawercode}",method=RequestMethod.GET)
    public String removeProfession(ModelMap model, @PathVariable String professioncode,@PathVariable String lawercode,RedirectAttributes redirectAttributes) {
        System.out.println("-----------"+professioncode+lawercode);
        apiService.removeProfession(professioncode,lawercode);

        this.successMessage(null, redirectAttributes);
        return "redirect:/profession/list";
    }


}
